package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.dict.AssetsType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.VoiceMemoir;
import com.ruoyi.ora.domain.VoiceRecord;
import com.ruoyi.ora.domain.VoiceStory;
import com.ruoyi.ora.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.AssetsCollect;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收藏Controller
 * 
 * @author liuwp
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/ora/assets_collect")
public class AssetsCollectController extends BaseController
{
    @Autowired
    private IAssetsCollectService assetsCollectService;
    @Autowired
    private IVoiceRecordService voiceRecordService;
    @Autowired
    private IVoiceMemoirService voiceMemoirService;
    @Autowired
    private IVoiceStoryService voiceStoryService;

    /**
     * 查询收藏列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetsCollect assetsCollect)
    {
        startPage();
        List<AssetsCollect> list = assetsCollectService.selectAssetsCollectList(assetsCollect);
        return getDataTable(list);
    }

    /**
     * 导出收藏列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetsCollect assetsCollect)
    {
        List<AssetsCollect> list = assetsCollectService.selectAssetsCollectList(assetsCollect);
        ExcelUtil<AssetsCollect> util = new ExcelUtil<AssetsCollect>(AssetsCollect.class);
        util.exportExcel(response, list, "收藏数据");
    }

    /**
     * 获取收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(assetsCollectService.selectAssetsCollectById(id));
    }

    /**
     * 新增收藏
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetsCollect assetsCollect)
    {
        return toAjax(assetsCollectService.insertAssetsCollect(assetsCollect));
    }

    /**
     * 修改收藏
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetsCollect assetsCollect)
    {
        return toAjax(assetsCollectService.updateAssetsCollect(assetsCollect));
    }

    /**
     * 删除收藏
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_collect:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assetsCollectService.deleteAssetsCollectByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('ora:assets_collect:list')")
    @GetMapping("/query")
    public TableDataInfo query(AssetsCollect assetsCollect)
    {
        startPage();
        assetsCollect.setUserId(SecurityUtils.getUserId());
        List<AssetsCollect> list = assetsCollectService.selectAssetsCollectList(assetsCollect);
        for (AssetsCollect collect : list) {
            if (collect.getCollectType().intValue() == AssetsType.VOICE.getValue()){
                collect.setVoiceRecord(voiceRecordService.selectVoiceRecordById(collect.getAssetsId()));
            }else if (collect.getCollectType().intValue() == AssetsType.MEMORI.getValue()){
                collect.setVoiceMemoir(voiceMemoirService.selectVoiceMemoirById(collect.getAssetsId()));
            }else if (collect.getCollectType().intValue() == AssetsType.STROY.getValue()){
                collect.setVoiceRecord(voiceRecordService.selectVoiceRecordById(collect.getAssetsId()));
            }
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ora:assets_collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addCollect(AssetsCollect assetsCollect)
    {
        assetsCollect.setUserId(SecurityUtils.getUserId());
        AssetsCollect assetsCollect1 = assetsCollectService.selectOneAssetsCollect(assetsCollect);
        if (assetsCollect1 == null){
            if (assetsCollect.getCollectType().intValue() == AssetsType.VOICE.getValue().intValue()){
                VoiceRecord voiceRecord = voiceRecordService.selectVoiceRecordById(assetsCollect.getAssetsId());
                voiceRecordService.updateVoiceRecord(Builder.of(VoiceRecord::new)
                        .with(VoiceRecord::setId, assetsCollect.getAssetsId())
                        .with(VoiceRecord::setCollectCount, voiceRecord.getCollectCount()==null?1:voiceRecord.getCollectCount() + 1)
                        .build());
            }
            if (assetsCollect.getCollectType().intValue() == AssetsType.MEMORI.getValue().intValue()){
                VoiceMemoir voiceMemoir = voiceMemoirService.selectVoiceMemoirById(assetsCollect.getAssetsId());
                voiceMemoirService.updateVoiceMemoir(Builder.of(VoiceMemoir::new)
                        .with(VoiceMemoir::setId, assetsCollect.getAssetsId())
                        .with(VoiceMemoir::setCollectCount, voiceMemoir.getCollectCount()==null?1:voiceMemoir.getCollectCount() + 1)
                        .build());
            }
            if (assetsCollect.getCollectType().intValue() == AssetsType.STROY.getValue().intValue()){
                VoiceStory voiceStory = voiceStoryService.selectVoiceStoryById(assetsCollect.getAssetsId());
                voiceStoryService.updateVoiceStory(Builder.of(VoiceStory::new)
                        .with(VoiceStory::setId, assetsCollect.getAssetsId())
                        .with(VoiceStory::setCollectCount, voiceStory.getCollectCount()==null?1:voiceStory.getCollectCount() + 1)
                        .build());
            }
            return toAjax(assetsCollectService.insertAssetsCollect(assetsCollect));
        }else {
            return AjaxResult.success(assetsCollect1);
        }
    }

}
